package com.sm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sm.entity.*;
import com.sm.service.AttendanceService;
import com.sm.service.FinancialService;
import com.sm.service.NewsService;
import com.sm.service.NoticeService;
import com.sm.utils.AttendanceUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class LoginController {

    @Autowired
    NewsService newsService;
    @Autowired
    NoticeService noticeService;
    @Autowired
    FinancialService financialService;
    @Autowired
    AttendanceService attendanceService;


    @RequestMapping(value = {"/","/index"})
    public String index(){
        return "index";
    }

    @RequestMapping("/login")
    public String login(){
//        System.out.println("登录界面");
        return "login";
    }
    @RequestMapping("/admin/main")
    public String main(Model model)throws Exception{
        //签到人数
        AttendanceRec now = attendanceService.atNow();
        Integer qiandao = now.getAtcount();
        model.addAttribute("qiandao",qiandao);
        //利润
        Double profit_today = financialService.todayProfit(AttendanceUtil.getDateStr());
        if(profit_today == null)profit_today =0.0;
        model.addAttribute("profit_today",profit_today);
        //账单数量
        Integer financial_count = financialService.findFinancialCount();
        model.addAttribute("financial_count",financial_count);
        //待处理消息数量
        Integer notice_count = 0;
        model.addAttribute("notice_count",notice_count);
        //新闻
        List<News> nowNews = newsService.findNowNews(7);
        model.addAttribute("nownews",nowNews);
        //账单
        List<Financial> nowFinancial = financialService.findNowFinancial(5,AttendanceUtil.getMonth());
        for (Financial financial:nowFinancial){
            System.out.println(financial);
        };
        model.addAttribute("nowfinancial",nowFinancial);
        //通告
        List<Notice> nowNotice = noticeService.findNowNotice(13);
        model.addAttribute("nownotice",nowNotice);

        System.out.println("系统管理登录成功");
        return "admin/main";
    }
    @RequestMapping("/bss/home")
    public String bss(){
        System.out.println("供销存系统登录成功");
        return "bssmanager/home";
    }

    @ResponseBody
    @RequestMapping("/newsindex")
    public ResultMap newsPages(@RequestParam(defaultValue = "1") Integer curr,@RequestParam(defaultValue = "10") Integer limit)throws Exception{
//        System.out.println(curr+"   \n\n\n\n\n    "+limit);
        PageHelper.startPage(curr,limit);
        List<News> news = newsService.findAllNews();
        PageInfo<News> pageInfo = new PageInfo(news);
        ResultMap resultMap = new ResultMap();
        resultMap.setResult("page",pageInfo);
        return resultMap;
    }

}
